package com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.CustomDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.AuthenticationCenterActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.ChatActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DateDetailsActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DetailInterfaceActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PicturePreviewActivity3;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.VipActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.RoundImageView;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.PayDialog;
import com.shehuan.niv.NiceImageView;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/27
 * 功能描述：约会详情界面的评论item
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */
public class DateDetailsAdapter extends BaseQuickAdapter<Map<String, String>, BaseViewHolder> {
    public String getSex, stringVip;
    public String getIdentification;
    public String getVip;
    public String token;


    public DateDetailsAdapter(int item_order_reminding) {
        super(item_order_reminding);
    }

    @Override
    protected void convert(BaseViewHolder helper, Map<String, String> item) {
        RoundImageView image_head = helper.itemView.findViewById(R.id.image_head);
        NiceImageView image_head_pic = helper.itemView.findViewById(R.id.image_head_pic);
        GlideUtils.glideFille(mContext, item.get("head_pic"), image_head);
        GlideUtils.glideFille(mContext, item.get("pic"), image_head_pic);
        AppCompatActivity me = (AppCompatActivity) mContext;
        getSex = Preferences.getInstance().getString(me, "user", "sex");
        getIdentification = Preferences.getInstance().getString(me, "user", "identification");
        getVip = Preferences.getInstance().getString(me, "user", "vip");
        token = Preferences.getInstance().getString(me, "user", "token");

        if (item.get("create_time") != null) {
            helper.setText(R.id.text_time, item.get("create_time"));
        }
        helper.setText(R.id.text_name, item.get("nickname"));
        TextView textTV = helper.itemView.findViewById(R.id.text_TA);
        getSex = Preferences.getInstance().getString(mContext, "user", "sex");
        stringVip = Preferences.getInstance().getString(mContext, "user", "vip");
        getIdentification = Preferences.getInstance().getString(mContext, "user", "identification");


        String id = item.get("id");
        String sex = item.get("sex");
        textTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(id) && TextUtils.isEmpty(sex)) {
                } else {
                    if (item.get("chat").equals("2")) {
                        liaotian(item.get("uuid"));
                        return;
                    }

                    if (getSex.equals(sex)) {
                        ToastUtil.show("不能私聊同性别的！");
                    } else {
                        if (getSex.equals("1")) {
                            //男
                            if (stringVip.equals("2")) {
                                dialogVip(item.get("money"), item.get("id"));
                            } else {
                                dialog(item.get("money"), "1", item.get("id"), "");
                            }
                        } else {
                            //女
                            if (getIdentification.equals("1")) {
                                dialog(item.get("money"), "1", item.get("id"), "");
                                return;
                            } else {
                                //已认证
                                liaotian(item.get("uuid"));
                                return;
                            }
                        }
                    }
                }

            }
        });
        image_head_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PicturePreviewActivity3.start(mContext, item.get("pic"));
            }
        });

        image_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(id) && TextUtils.isEmpty(sex)) {
                } else {
                    if (getSex.equals(sex)) {
                        ToastUtil.show("不能查看同性别的！");
                    } else {
                        panduan(id);
                    }
                }
            }
        });
    }

    private void dialogVip(String money, String cover_member_id) {
        AppCompatActivity me = (AppCompatActivity) mContext;
        Model.getInstance().getUserApi().isPay(token, cover_member_id, "1", "1")
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        if (data == null) {
                            return;
                        }
                        if (data.get("vip_count").equals("0")) {
                            //付费支付
                            dialog(money, "1", cover_member_id, "1");
                            return;
                        }

                        MessageDialog.show(me,
                                "会员",
                                "是否使用剩余次数:" + data.get("vip_count"),
                                "确认", "取消")
                                .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                    @Override
                                    public boolean onClick(BaseDialog baseDialog, View v) {
                                        Model.getInstance().getUserApi().isPay(token, cover_member_id, "1", "2")
                                                .enqueue(new Callback<JsonObject>() {
                                                    @Override
                                                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                                                        Map<String, String> map = Constants.codeGet(me, response);
                                                        if (map == null) {
                                                            return;
                                                        }
                                                        ChatActivity.start(mContext, cover_member_id + "m", 1);
                                                    }

                                                    @Override
                                                    public void onFailure(Call<JsonObject> call, Throwable t) {

                                                    }
                                                });
                                        return false;
                                    }
                                });

                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    private void liaotian(String uuid) {
        ChatActivity.start(mContext, uuid, 1);
    }

    //点击头像判断
    private void panduan(String id) {
        AppCompatActivity me = (AppCompatActivity) mContext;
        //                             是否vip 1否 2是           1未认证 2 已认证
        if (getSex.equals("1")) {
            if (getVip.equals("1")) {
                HttpRequest.POST(mContext, "Index/seeCount"
                        , new Parameter()
                                .add("token", token)
                                .add("cover_member_id", id)
                        , new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {
                                Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                if (data.get("look").equals("1")) {
                                    DetailInterfaceActivity.start(me, id, "1");
                                    return;
                                }
                                int count = Integer.valueOf(data.get("count"));
                                if (count <= 3 && count > 0) {
                                    //立即开通
                                    MessageDialog.show(me, "剩余机会" + data.get("count"), "非会员每天只能查看" + data.get("total") + "位女士", "升级会员", "继续查看")
                                            .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                                @Override
                                                public boolean onClick(BaseDialog baseDialog, View v) {
                                                    VipActivity.start(me, "1", 1);
                                                    return false;
                                                }
                                            })
                                            .setOnCancelButtonClickListener(new OnDialogButtonClickListener() {
                                                @Override
                                                public boolean onClick(BaseDialog baseDialog, View v) {
                                                    DetailInterfaceActivity.start(me, id, "1");
                                                    return false;
                                                }
                                            })
                                    ;
                                } else if (count == 0) {
                                    ToastUtil.show("您没有次数了");
                                } else {
                                    DetailInterfaceActivity.start(me, id, "1");
                                }
                            }
                        });
            } else {
                //会员
                DetailInterfaceActivity.start(me, id, "1");
            }
        } else {
            if (getIdentification.equals("1")) {
                HttpRequest.POST(me, "Index/seeCount"
                        , new Parameter()
                                .add("token", token)
                                .add("cover_member_id", id)
                        , new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {
                                Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                int count = Integer.valueOf(data.get("count"));
                                if (data.get("look").equals("1")) {
                                    DetailInterfaceActivity.start(me, id, "1");
                                    return;
                                }
                                if (count <= 3 && count > 0) {
                                    //立即开通
                                    MessageDialog.show(me, "剩余机会" + data.get("count"), "未认证每天只能查看" + data.get("total") + "位男士", "去认证", "继续查看")
                                            .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                                @Override
                                                public boolean onClick(BaseDialog baseDialog, View v) {
                                                    //去认证
                                                    Intent intent = new Intent(me, AuthenticationCenterActivity.class);
                                                    Bundle bundle = new Bundle();
                                                    bundle.putInt("dataSize", 1);
                                                    intent.putExtras(bundle);
                                                    me.startActivity(intent);
                                                    return false;
                                                }
                                            })
                                            .setOnCancelButtonClickListener(new OnDialogButtonClickListener() {
                                                @Override
                                                public boolean onClick(BaseDialog baseDialog, View v) {
                                                    DetailInterfaceActivity.start(me, id, "1");
                                                    return false;
                                                }
                                            })
                                    ;
                                } else if (count == 0) {
                                    ToastUtil.show("您没有次数了请先认证");
                                    return;
                                } else {
                                    DetailInterfaceActivity.start(me, id, "1");
                                }
                            }
                        });
            } else {
                DetailInterfaceActivity.start(me, id, "1");
            }
        }
    }

    //点击购买的非会员的
    private void dialog(String set_info_money, String type, String memberId, String wode) {
        AppCompatActivity me = (AppCompatActivity) mContext;
        CustomDialog.show(me, R.layout.dialog_chat, new CustomDialog.OnBindView() {
            @Override
            public void onBind(final CustomDialog dialog, View v) {
                TextView text_vip = v.findViewById(R.id.text_vip);
                TextView text_title = v.findViewById(R.id.text_title);
                TextView text_context = v.findViewById(R.id.text_context);
                TextView text_private_chat = v.findViewById(R.id.text_private_chat);
                View view_gone = v.findViewById(R.id.view_gone);


                if (getSex.equals("1")) {
                    if (getVip.equals("2")) {
                        text_context.setVisibility(View.GONE);
                        text_vip.setVisibility(View.GONE);
                    }
                    text_title.setText("联系她");
                    text_context.setText("想看他的全部资料和私聊她,成为会员，\n" +
                            "免费私聊");
                } else {
                    text_title.setText("联系他");
                    text_context.setText("想看他的全部资料和私聊他,认证之后，\n" +
                            "免费私聊");
                    text_vip.setText("立即认证");
                }
                text_private_chat.setText("付费查看和私聊(￥" + set_info_money + ")");
                if (wode == null) {
                    text_context.setVisibility(View.VISIBLE);
                    text_vip.setVisibility(View.VISIBLE);
                    view_gone.setVisibility(View.VISIBLE);
                }else {
                    text_context.setVisibility(View.GONE);
                    text_vip.setVisibility(View.GONE);
                    view_gone.setVisibility(View.GONE);
                }


                TextView text_center = v.findViewById(R.id.text_center);
                text_center.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                    }
                });
                text_private_chat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PayDialog.start(me, set_info_money, type, memberId);
                        dialog.doDismiss();
                    }
                });
                text_vip.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (getSex.equals("1")) {
                            //购买会员
                            VipActivity.start(me, "1", 1);
                        } else {
                            //认证
                            Intent intent = new Intent(me, AuthenticationCenterActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("dataSize", 2);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                        }
                        dialog.doDismiss();
                    }
                });
            }
        });
    }
}